<?php
// 授权登录
namespace app\shop\controller;

use think\Db;
use think\Session;
use think\Request;
use think\Controller;
use think\Exception;
use app\shop\model\Member as MemberModel;
use app\shop\model\MemberBrokerage as MemberBrokerageModel;

class AuthorizedLogin extends Controller
{

    /**
     * 微信登录
     */
    public function wechat_login()
    {
        // 判断是否get提交
        if (Request::instance()->isGet()) {
            // 获取参数
            $data = Request::instance()->param();
            // halt($data);
            // 拼装url路径 获取access_token
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx4c0b834c512dcb56&secret=8335244b58c9115f2a7a9cb81ceeb903&code=" . $data['code'] . "&grant_type=authorization_code";
            // 访问路径 获取access_token 数据
            $tokenJson = curl_get_contents($url);
            // halt($tokenJson);
            // 杰森转数组
            $tokenArr = json_decode($tokenJson, true);
            // 判断异常
            if (array_key_exists('errmsg', $tokenArr)) throw new Exception($tokenArr['errmsg']);

            // 获取用户信息
            $memberUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $tokenArr['access_token'] . "&openid=" . $tokenArr['openid'] . "&lang=zh_CN";
            $memberJson = curl_get_contents($memberUrl);
            $memberArr = json_decode($memberJson, true);

            // 调用第三方登录
            self::third_login($memberArr, 'wechat_id');
        }
    }

    /*
      * 登录|注册第三方账号
      */
    public function third_login($info,$login_type)
    {
        $model = new MemberModel();
        //  $login_type ['wechat_id','weibo_id','qq_id'];
        $arr = $model->where($login_type,$info['unionid'])->find();
        if ($arr){
            $map['login_time'] = time();
            $map['login_ip'] = Request::instance()->ip();
            $map['id'] = $arr['id'];
            $map['password'] = '';
            $result = $model->isUpdate(true)->save($map);
            Session::set('member_avatar',$arr['avatar']);
            Session::set('member_id',$arr['id']);
            Session::set('member_name',$arr['nickname']);
            if (!$result) throw new Exception('登录异常,请重试');
            $this->redirect('shop/index/index');  //跳转到我的
        }else{   //去注册
            $ini['nickname'] = removeEmoji($info['nickname']);
            $ini['password'] = '';
            $ini['wechat_id'] = $info['unionid'];
            $ini['avatar'] = $info['headimgurl'];
            $ini['create_time'] = time();
            $ini['create_ip'] = Request::instance()->ip();
            $ini['username'] = ' ';
            $ini['password'] = ' ';
            // $model->allowField(true)->save($ini);
            $uid = Db::name('member')->insertGetId($ini);
            Session::set('member_id',$uid);
            //附加会员佣金
            self::add_brokerage($uid);
            $this->redirect('shop/login/personal');  //跳转到我的
        }
    }
    /*
     * 附加会员佣金表
     */
    public function add_brokerage($member_id)
    {
        $model = new MemberBrokerageModel();
        $map['member_id'] = $member_id;
        $map['general_income'] = 0 ;
        $map['to_yield'] = 0 ;
        $map['to_audit'] = 0 ;
        $map['actual_amount'] = 0 ;
        $model->save($map);
    }

}